package com.yuan.rest.controller;

import com.yuan.rest.entrity.Dept;
import com.yuan.rest.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: SiYuan
 * @Date: 2019/1/10 17:06
 * @Description:
 */
@Controller
public class PageController {
    @Autowired
    DeptRepository deptRepository;

    @GetMapping("/page/{page}/{size}")
    @ResponseBody
    public  List<Dept>  getpage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
         Sort sort = new Sort(Sort.Direction.DESC,"name");
          //构造PageRequest分页条件，第一页、每页10行、排序条件
          PageRequest pageRequest = new PageRequest(page,size,sort);
          Page<Dept> deptPage = deptRepository.findAll(pageRequest);
          List<Dept> deptList = deptPage.getContent();
          return deptList;
     }
}
